package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.CodeUrlBean;
import com.sskj.cndragon.presenter.SpreadActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.QRCodeUtil;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SpreadActivity extends BaseActivity<SpreadActivityPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.iv_qrode)
    ImageView ivQrode;
    @BindView(R.id.tv_first)
    TextView tvFirst;
    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.tv_third)
    TextView tvThird;
    @BindView(R.id.tv_fourth)
    TextView tvFourth;
    @BindView(R.id.tv_fifth)
    TextView tvFifth;
    @BindView(R.id.tv_sixth)
    TextView tvSixth;
    @BindView(R.id.tv_url)
    TextView tvUrl;
    @BindView(R.id.btn_copy)
    Button btnCopy;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_spread;
    }

    @Override
    public SpreadActivityPresenter getPresenter() {
        return new SpreadActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, btnCopy, new ClickUtil.Click() {
            @Override
            public void click() {
                if (!isEmpty(tvUrl)) {
                    CopyUtil.copy(getText(tvUrl));
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getCodeUrl();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SpreadActivity.class);
        context.startActivity(intent);
    }

    public void getCodeUrlOK(CodeUrlBean codeUrlBean) {
        if (codeUrlBean != null) {
//            ImageUtil.setImage(codeUrlBean.getCodeUrlImg(), ivQrode);
            QRCodeUtil.createImage(codeUrlBean.getCodeUrl(), (int) RudenessScreenHelper.pt2px(this, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
                @Override
                public void onAnalyzeSuccess(Bitmap bitmap) {
                    ivQrode.setImageBitmap(bitmap);
                }

                @Override
                public void onAnalyzeFailed() {

                }
            });

            tvUrl.setText(codeUrlBean.getCodeUrl());
            if (codeUrlBean.getCode().length() >= 6) {
                tvFirst.setText(codeUrlBean.getCode().charAt(0) + "");
                tvSecond.setText(codeUrlBean.getCode().charAt(1) + "");
                tvThird.setText(codeUrlBean.getCode().charAt(2) + "");
                tvFourth.setText(codeUrlBean.getCode().charAt(3) + "");
                tvFifth.setText(codeUrlBean.getCode().charAt(4) + "");
                tvSixth.setText(codeUrlBean.getCode().charAt(5) + "");
            }
        }
    }
}
